package com.fsc.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fsc._enum.ReturnEnum;
import com.fsc.entity.ResultWrapper;
import com.fsc.entity.domain.*;
import com.fsc.entity.dto.EvaluationDTO;
import com.fsc.entity.dto.RewardPushDTO;
import com.fsc.entity.vo.ClassInfoVO;
import com.fsc.entity.vo.EvaluationVO;
import com.fsc.entity.vo.RewardPunishVO;
import com.fsc.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.ognl.Evaluation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: RavenLyn
 * @Date: 2024-06-27
 */
@RestController
@RequestMapping("/evaluation")
@Slf4j
public class EvaluationController {

    @Autowired
    private EvaluationInfoService evaluationService;
    @Autowired
    private EvaluateStuInfoService evaluateStuInfoService;
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private LoginInfoService loginInfoService;
    @Autowired
    private StuPartTeachService stuPartTeachService;
    @Autowired
    private StudentInfoService studentInfoService;

    /**
     * 获取评价总表名称
     * @return
     */
    @GetMapping("/titles")
    public ResultWrapper<List<EvaluationInfo>> getTitles(){
        List<EvaluationInfo> titleInfo = evaluationService.list();
        return ResultWrapper.success(ReturnEnum.SUCCESS,titleInfo);
    }

    /**
     * 获取学生评价信息
     * @return
     */
    @GetMapping("/all")
    public ResultWrapper<List<EvaluationVO>> getAll(int evaluationId){
        Integer loginId = Integer.parseInt((String) StpUtil.getLoginId());
        int role = loginInfoService.getRole(loginId);
        QueryWrapper<EvaluateStuInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("evaluation_id",evaluationId);
        List<EvaluateStuInfo> evaluationList = evaluateStuInfoService.list(wrapper); //先获取本次评价的所有id以及内容
        List<Integer> idList = new ArrayList<>(); //idList是loginId对应的学生id

        if(role == 0){ //领导 本次评价的所有学生
            for (int i = 0; i < evaluationList.size(); i++) {
                idList.add(evaluationList.get(i).getStudentId());
            }
        }
        List<EvaluateStuInfo> evaluationList1 = new ArrayList<>();
        if(role == 1){ //老师 本次评价自己教的所有学生
            QueryWrapper<StuPartTeach> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("teacher_id",loginId);
            List<StuPartTeach> list1 = stuPartTeachService.list(wrapper1);//只取id

            for (int i = 0; i < list1.size(); i++) {
                idList.add(list1.get(i).getStudentId());
            }
            //根据idList和evaluation_id去重新获取本次评价的内容
            QueryWrapper<EvaluateStuInfo> wrapper2 = new QueryWrapper<>();
            wrapper2.in("student_id",idList);
            wrapper2.eq("evaluation_id",evaluationId);
            evaluationList1 = evaluateStuInfoService.list(wrapper2);
            for (int i = 0; i < evaluationList1.size(); i++) {
                System.out.println(evaluationList1.get(i));
            }
        }
        if(role == 2){ //家长 本次评价自己的所有孩子
            QueryWrapper<StuPartTeach> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parent_id",loginId);
            List<StuPartTeach> list1 = stuPartTeachService.list(wrapper1);//只取id
            for (int i = 0; i < list1.size(); i++) {
                idList.add(list1.get(i).getStudentId());
            }
            //根据idList去重新获取本次评价的内容
            QueryWrapper<EvaluateStuInfo> wrapper2 = new QueryWrapper<>();
            wrapper2.in("student_id",idList);
            wrapper2.eq("evaluation_id",evaluationId);
            evaluationList1 = evaluateStuInfoService.list(wrapper2);
        }

        List<Integer> idListFinal = new ArrayList<>(); //idListFinal是本次评价的所有id（包括重复的）

        if(role == 0){
            idListFinal = idList;
        }
        if(role != 0){
            for (int i = 0; i < evaluationList1.size(); i++) {
                idListFinal.add(evaluationList1.get(i).getStudentId());
                System.out.println(idListFinal.get(i));
            }
        }

        //用idListFinal去stu_part_teach表获取classIds
        int[] classIds = new int[idListFinal.size()];
        for (int i = 0; i < idListFinal.size(); i++) {
            classIds[i] = stuPartTeachService.getClassIdByStuId(idListFinal.get(i));
            System.out.println(classIds[i]);
        }

        //用idListFinal去student_info表获取studentName
        QueryWrapper<StudentInfo> wrapper3 = new QueryWrapper<>();
        wrapper3.in("student_id",idListFinal);
        List<StudentInfo> nameList = studentInfoService.list(wrapper3);
        for (int i = 0; i < nameList.size(); i++) {
            System.out.println(nameList.get(i));
        }

        List<EvaluationVO> voList = new ArrayList<>();
        //EvaluationVO: id studentId studentName description classId;
        if(role == 0){ //领导
            for (int i = 0; i < idListFinal.size(); i++) {
                EvaluationVO newOne = new EvaluationVO();
                BeanUtils.copyProperties(evaluationList.get(i),newOne);//id studentId description
                newOne.setClassId(classIds[i]);//classId
                for (StudentInfo studentInfo : nameList) {
                    if (newOne.getStudentId() == studentInfo.getStudentId()) {
                        newOne.setStudentName(studentInfo.getStudentName());// studentName
                    }
                }
                voList.add(newOne);
                System.out.println(voList.get(i));
            }
        }
        if(role != 0){ //非领导
            for (int i = 0; i < idListFinal.size(); i++) {
                EvaluationVO newOne = new EvaluationVO();
                BeanUtils.copyProperties(evaluationList1.get(i),newOne);//id studentId description
                newOne.setClassId(classIds[i]);//classId
                for (StudentInfo studentInfo : nameList) {
                    if (newOne.getStudentId() == studentInfo.getStudentId()) {
                        newOne.setStudentName(studentInfo.getStudentName());// studentName
                    }
                }
                voList.add(newOne);
                System.out.println(voList.get(i));
            }
        }
        return ResultWrapper.success(ReturnEnum.SUCCESS,voList);
    }

    /**
     * 获取班级数据
     * @return
     */
    @GetMapping("/classes")
    public ResultWrapper<List<ClassInfoVO>> getClasses() {
        List<ClassInfo> list = new ArrayList<>();
        Integer loginId = Integer.parseInt((String) StpUtil.getLoginId());
        int role = loginInfoService.getRole(loginId);
        if(role == 0){ //领导
            list = classInfoService.list();
        }
        if(role == 1){ //老师
            list = classInfoService.getClassByTeaId(loginId);
        }

        List<ClassInfoVO> classesList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++){
            ClassInfoVO newOne = new ClassInfoVO();
            BeanUtils.copyProperties(list.get(i),newOne);
            classesList.add(newOne);
        }
        return ResultWrapper.success(ReturnEnum.SUCCESS,classesList);
    }


    /**
     * 更新学生评价信息
     * @param id
     * @param description
     */
    @PutMapping("/update")
    public void updateInfo(int id,String description){

        UpdateWrapper<EvaluateStuInfo> wrapper = new UpdateWrapper<>();
        wrapper.set("description",description)
                .eq("id",id);
        evaluateStuInfoService.update(wrapper);
    }

    /**
     * 删除学生评价信息
     * @param id
     */
    @DeleteMapping("/delete")
    public void deleteInfo(int id){
        QueryWrapper<EvaluateStuInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        evaluateStuInfoService.remove(wrapper);
    }

    @GetMapping("/names")
    public ResultWrapper<List<EvaluationVO>> getStuNames(){
        Integer loginId = Integer.parseInt((String) StpUtil.getLoginId());
        int role = loginInfoService.getRole(loginId);
        List<StudentInfo> stuInfoList = null;
        if(role == 0){ //领导
            stuInfoList = studentInfoService.list();
        }
        if(role == 1){ //老师
            List<Integer> idList = stuPartTeachService.getStudent(loginId);
            QueryWrapper<StudentInfo> wrapper = new QueryWrapper<>();
            wrapper.in("student_id",idList);
            stuInfoList = studentInfoService.list(wrapper);
        }

        //封装voList
        List<EvaluationVO> voList = new ArrayList<>();
        for (int i = 0; i < stuInfoList.size(); i++) {
            EvaluationVO newOne = new EvaluationVO();
            BeanUtils.copyProperties(stuInfoList.get(i),newOne); //拷贝stuInfoList中的studentId和studentName
            voList.add(newOne);
        }
        return ResultWrapper.success(ReturnEnum.SUCCESS,voList);
    }

    @PostMapping("/add_stu")
    public void addStuInfo(@RequestBody EvaluationDTO evaluationDTO){
        int studentId = evaluationDTO.getStudentId();
        String description = evaluationDTO.getDescription();
        int evaluationId = evaluationDTO.getEvaluationId();

        EvaluateStuInfo newOne = new EvaluateStuInfo();
        newOne.setStudentId(studentId);
        newOne.setDescription(description);
        newOne.setEvaluationId(evaluationId);
        evaluateStuInfoService.save(newOne);
    }

    @PostMapping("/add_title")
    public void addTitle(String title){
        EvaluationInfo newOne = new EvaluationInfo();
        newOne.setTitle(title);
        newOne.setTime(Date.valueOf(LocalDate.now()));
        boolean test = evaluationService.save(newOne);
        System.out.println(test);

    }

    @DeleteMapping("/delete_titles")
    public void deleteTitle(int[] titleIds){
        List<Integer> ids = new ArrayList<>();
        for (int i = 0; i < titleIds.length; i++) {
            ids.add(titleIds[i]);
            System.out.println(ids.get(i));
        }

        QueryWrapper<EvaluationInfo> wrapper = new QueryWrapper<>();
        wrapper.in("id",ids);
        boolean test = evaluationService.remove(wrapper);
        System.out.println(test);

//        if(titleIds.size() == 1){
//            wrapper.eq("id",titleIds.get(0));
//            boolean test = evaluationService.remove(wrapper);
//            System.out.println(test);
//        }
//        if(titleIds.size() > 1){
//
//        }

    }
}
